import Vue from 'vue'
import Vuex from 'vuex'
import { LoginAPI } from '@/api/login'
Vue.use(Vuex)

export default new Vuex.Store({
  // 严格模式，一切数据的修改 只能通过 mutations 修改
  strict: true,
  state: {
    token: localStorage.getItem('token') || '',
    refreshToken: localStorage.getItem('refresh_token') || ''
  },
  mutations: {
    setToken (state, tokenStr) {
      state.token = tokenStr
      localStorage.setItem('token', state.token)
    },
    setRefresh (state, retoken) {
      state.refreshToken = retoken
      localStorage.setItem('refresh_token', state.refreshToken)
    }
  },
  actions: {
    async  asyncLoginAction (store, formLogin) {
      const res = await LoginAPI(formLogin)
      const { token, refresh_token: refreshToken } = res.data.data
      store.commit('setToken', token)
      store.commit('setRefresh', refreshToken)
    }
  },
  modules: {
  }
})
